/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.IPage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.crm.service.ISloggerService;
import com.crm.model.User;
import com.crm.model.SysLog;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class SysOptRecord extends BasePage implements PageBeginRenderListener {

    private static Log logger = LogFactory.getLog(SysOptRecord.class);
    private String target;

    @InjectSpring("sloggerService")
    public abstract ISloggerService getSloggerService();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("SysDelHis")
    public abstract SysDelHis getSysDelHis();

    public abstract Date getFromDate();

    public abstract void setFromDate(Date fromDate);

    public abstract Date getToDate();

    public abstract void setToDate(Date toDate);

    public void pageBeginRender(PageEvent event) {
        if (getFromDate() == null) {
            setFromDate(new Date());
        }
        if (getToDate() == null) {
            setToDate(new Date());
        }
        if (getUserSession().getUsername() == null || !getUserSession().getUserGroup().equals(User.GROUP_SUPER)) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        } else if (target == null) {
            target = SysLog.T_CUST;
        }
    }

    public List getEntries() {
        try {
            return getSloggerService().getOperateRecord(target, getFromDate(), getToDate()) ;
        }
        catch (Exception e) {
            String exceptionString = "The exception happened when list operating record. " + e.getMessage();
            logger.error(exceptionString);
            e.printStackTrace();
            return new ArrayList();
        }
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public IPage switchTarget() {
        return this;
    }

    public IPage onShowDelHis(String uid) {
        SysDelHis sysDelHis = getSysDelHis();
        sysDelHis.setUid(uid);
        return sysDelHis;
    }

}
